<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>语种管理页面</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>


<table class="layui-hide" id="languageObject_table" lay-filter="languageObject_table" lay-skin="line"></table>
<div id="laypage" class="$('.layui-laypage-btn').click();"></div>
<!--增加修改模块--->
<div id="edit-user-info" style="display:none;">
    <form class="layui-form" action="" style="margin-top: 10px;margin-left: 10px;margin-right: 10px;" lay-filter="languageEditFilter" id="languageEditFilter" autocomplete="off">
    <div style="margin-left: 100px;margin-top: 20px;">
        <input name="id" hidden/>
        <div class="layui-inline">
            <div class="layui-form-label">语种名称</div>
            <div class="layui-input-block">
                <input type="text" name="languageName" placeholder="语种名称" class="layui-input">
            </div>
        </div><br/>
        <div class="layui-inline">
            <div class="layui-form-label">类型</div>
            <div class="layui-input-block">
                <input type="text" name="type" placeholder="" class="layui-input">
            </div>
        </div><br/>
        <div class="layui-inline">
            <div class="layui-form-label">备注</div>
            <div class="layui-input-block">
                <input type="text" name="remarks" placeholder="备注" class="layui-input">
            </div>
        </div>


    </div>
        <div class="layui-form-item">
            <div style="text-align: center;">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="edit-language-submit">保存</button>
                <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/html" id="toolbar">
    <div class="layui-btn-group">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="addNewStudy" shiro:hasPermission="sys:language:add">
            <i class="layui-icon">&#xe608;</i> 新增
        </button>
    </div>

    <div class="layui-btn-group">
        <button type="button" class="layui-btn layui-btn-sm" lay-event="buffDelete" shiro:hasPermission="sys:language:DELETED">
            <i class="layui-icon">&#xe640;</i> 批量删除
        </button>
    </div>
    </script>
<script type="text/html" id="tool">

    <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="sys:language:update">编辑</a>

</script>

<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    let $ = jQuery = layui.jquery;
    let table = layui.table
        , laypage = layui.laypage
        , form = layui.form
        , layer = layui.layer
        , laydate = layui.laydate
        , tree = layui.tree;
    layui.use(['table', 'laypage', 'layer', 'laydate', 'tree'], function () {

        //搜索条件
        let searchParam = {
            page: 1,
            pageSize: 10
        };




        $(document).ready(function(){

            //分页获取列表信息接口
            CoreUtil.sendAjax("/dLanguage/selectLanguageIsPage", searchParam, function(res) {
                // console.log(res);
                laypageTable(res.data.total, searchParam.page);
                if (res.data.records != null) {
                    loadTable(res.data.records);
                }
            }, "GET", false, function (res) {
                layer.msg("抱歉！您暂无获取列表的权限");
                let noAuthorityData = [];
                loadTable(noAuthorityData);
            });
        });

        //分页条
        let laypageTable = function (count, currentPage) {
            laypage.render({
                elem: 'laypage'
                , count: count
                , limit: searchParam.pageSize
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                , curr: location.hash.replace('#!currentPage=', '') //获取起始页
                , hash: 'currentPage' //自定义hash值
                , limits: [10, 25, 50, 100, 300, 500, 2000]
                , jump: function (obj, first) {
                    if (!first) {
                        searchParam.page = obj.curr;
                        searchParam.pageSize = obj.limit;
                        CoreUtil.sendAjax("/dLanguage/selectLanguageIsPage", searchParam, function (res) {
                            if (res.data.records != null) {
                                loadTable(res.data.records);
                                laypageTable(res.data.total, searchParam.page);
                            }
                        }, "GET", false, function (res) {
                            layer.msg("抱歉！您暂无获取列表信息的权限");
                            let noAuthorityData = [];
                            loadTable(noAuthorityData);
                        });

                    }
                }
            });
        };

        //渲染table
        let loadTable = function (data) {
            table.render({
                elem: '#languageObject_table'
                , cols: [
                    [ {type: 'checkbox', fixed: 'left'},
                        {type: 'id', fixed: 'left', title: '序号', width: '10%', type: 'numbers'},
                        {field: 'languageName', title: '语种等级', width: '20%', event: 'languageName', style:'cursor: pointer;' },
                        {field: 'type', title: '类型', width: '30%', event: 'type', style:'cursor: pointer;'},
                        {field: 'remarks', title: '备注', width: '28%', event: 'remarks', style:'cursor: pointer;'},
                        {width: '8%', toolbar: "#tool", title: '操作'}
                    ]
                ]
                , data: data
                , loading: true
                , even: true
                , limit: data.length
                , toolbar: '#toolbar'
            });
        };
        //监听表格头部工具栏事件
        table.on('toolbar(languageObject_table)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'addNewStudy':
                    //重置表单数据
                    $("#languageEditFilter")[0].reset();
                    form.render();
                    //打开弹框
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        resize: false, //是否允许拉伸
                        maxmin: false, //最大最小化
                        title: "新增信息",
                        area: ['100%', '100%'],
                        content: $("#edit-user-info") //引用的弹出层的页面层的方式加载修改界面表单
                    });
                    //初始化部门树型
                    break;
                case 'batchDeleted':
                    let checkStatus = table.checkStatus(obj.config.id);
                    let data2 = checkStatus.data;
                    if (data2.length == 0) {
                        layer.msg("请选择要删除的用户");
                    } else {
                        let userIds = [];
                        $(data2).each(function (index, item) {
                            userIds.push(item.id);
                        });
                        tipDialog(userIds, "");
                    }
                    break;
                case 'buffDelete':
                    let checkStatuss = table.checkStatus(obj.config.id);
                    let data2s = checkStatuss.data;
                    if (data2s.length == 0) {
                        layer.msg("请选择要删除的");
                    } else {
                        let userIdss = [];
                        $(data2s).each(function (index, item) {
                            userIdss.push(item.id);
                        });
                        tipforbidDialog(userIdss, "");
                    }
                    break;
                case 'LAYTABLE_TIPS':
                    layer.msg("这是工具栏右侧自定义的一个图标按钮");
                    break;
            }
        });
        //监听行工具事件
        table.on('tool(languageObject_table)', function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case 'batdel':
                    let userIds = [];
                    userIds.push(data.id);
                        sureDelete(userIds);//删除


                    break;
                case 'edit':
                    layer.open({
                        //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                        type: 1,
                        resize: false, //是否允许拉伸
                        maxmin: false, //最大最小化
                        title: "修改信息",
                        area: ['100%', '100%'],
                        content: $("#edit-user-info") //引用的弹出层的页面层的方式加载修改界面表单
                    });
                    //回显数据，填充表格
                    setFormValue(obj);
                    //初始化部门树型
                    initTree("");
                    break;
            }
        });

        //回显数据，填充表格
        function setFormValue(obj) {
            // console.log(obj);
            let data = obj.data;
            let status_value = true;
            if (data.status === 2) {
                status_value = false;
            }
            form.val("languageEditFilter", {
                "languageName": data.languageName
                , "id": data.id
                , "type": data.type
                , "remarks": data.remarks


            });
            form.render();
        }
        //是否批量删除确认框
        let tipforbidDialog = function (userIds, username) {
            layer.open({
                content: '确定要删除该等级吗?',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendAjax("/dLanguage/deleteLanguage", JSON.stringify(userIds), function (res) {
                        layer.msg(res.msg);
                        $(".layui-laypage-btn").click();
                    }, "DELETE", false, function (res) {
                        layer.msg(res.msg);
                    });
                }
            });
        };

        //显示建议的详细信息
        let suggestiondetail = function(id){

            CoreUtil.sendAjax("/dLanguage/selectLanguageIsPage/" + id,null,function(res) {

                 $("#username").html(res.data.username);
                 $("#phone").html(res.data.phone);
                 $("#email").html(res.data.email);
                 $("#update_time").html(res.data.updateTime);
                 $("#contents").html(res.data.content);

            }, "POST", false, function (res) {
                console.log(res);
                layer.msg(res.msg);
            });
            layer.open({
                //layer提供了5种层类型。可传入的值有：0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
                type: 1,
                resize: false, //是否允许拉伸
                maxmin: false, //最大最小化
                title: "建议详情",
                area: ['40%', '60%'],//宽，高
                shadeClose: true, //点击弹层外区域关闭
                content: $("#userInfoDetailDiv")
            });
        };

        //监听提交-编辑/新增用户信息弹框
        form.on('submit(edit-language-submit)', function (data) {
   // var level=   $("#level").value;
   // alert(level)
   //          if(!isNaN(level)){
   //              //对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
   //              //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
   //
   //              return true;
   //          }
   //
   //          else{  layer.msg("等级只能是数字");
   //              return false;
   //          }

            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendAjax("/dLanguage/addLanguage", JSON.stringify(data.field), function (res) {
                    layer.closeAll(); //疯狂模式，关闭所有层
                    layer.msg(res.msg);
                    $(".layui-laypage-btn").click();
                }, "POST", false, function (res) {
                    layer.msg(res.msg);

                });
            } else {
                CoreUtil.sendAjax("/dLanguage/updateLanguage", JSON.stringify(data.field), function (res) {
                    layer.closeAll(); //疯狂模式，关闭所有层
                    $(".layui-laypage-btn").click();

                }, "PUT", false, function (res) {
                    alert(222)
                    layer.closeAll(); //疯狂模式，关闭所有层
                    layer.msg(res.msg);
                });
            }
            return false;

        });
        //是否删除确认框
        let sureDelete = function (pkId) {
            layer.open({
                content: '确定要删除该记录吗?',
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    $.ajax({
                        type: "DELETE",
                        url: "/dLanguage/deleteLanguage?id="+pkId,
                        dataType: "json",
                        success: function(res){
                            layer.msg(res.message);
                            $(".layui-laypage-btn").click();
                        }, error: function (){
                            layer.msg(res.message);
                        }
                    });


                }
            });
        };
    });
</script>